﻿using FriendStar.Application.MainData;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class UnloadingPlaceController : ControllerBase
    {
        //
        // GET: /MainData/UnloadingPlace/
        ZSJ_B1_GH_SupplierInformationApp zsj_b1_gh_supplierinformationApp = new ZSJ_B1_GH_SupplierInformationApp();
        V_ZSJ_D1_UnloadPlaceApp v_zsj_d1_unloadplaceApp = new V_ZSJ_D1_UnloadPlaceApp();
        ZSJ_D1_UnloadPlaceApp zsj_d1_unloadplaceApp = new ZSJ_D1_UnloadPlaceApp();

        [HttpGet]
        [HandlerAuthorize]
        public override ActionResult Index()
        {
            ViewBag.custerComNO = zsj_b1_gh_supplierinformationApp.GetList("IsCustomer ='1'");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUnloadingPlaceJson(Pagination pagination, string UnloadPlaceNO, string UnloadPlaceName, string FCusterComNO, string FHappenComNO)
        {
            var data = new
            {
                rows = v_zsj_d1_unloadplaceApp.GetList(pagination, UnloadPlaceNO, UnloadPlaceName, FCusterComNO, FHappenComNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUnloadingPlaceSortJson(string sidx, string sord, int rows, string UnloadPlaceNO, string UnloadPlaceName, string FCusterComNO, string FHappenComNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_d1_unloadplaceApp.GetList(pagination, UnloadPlaceNO, UnloadPlaceName, FCusterComNO, FHappenComNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].UnitID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitUnloadingPlaceForm(string status, ZSJ_D1_UnloadPlaceEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (zsj_d1_unloadplaceApp.CheckedEntity(entity))
                {
                    zsj_d1_unloadplaceApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.UnitID);
                }
                else
                {
                    return Error("新增失败，已经存在相同卸货地点编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_d1_unloadplaceApp.CheckedEntity(entity))
                {
                    ZSJ_D1_UnloadPlaceEntity oldEntity = zsj_d1_unloadplaceApp.GetForm(entity.UnitID);
                    if (oldEntity != null)
                    {
                        zsj_d1_unloadplaceApp.SubmitForm(entity, entity.UnitID);
                        return Success("修改操作成功。", entity.UnitID);
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的卸货地点数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同卸货地点编号数据。");
                }
            }
            else if (status == "D")
            {

                ZSJ_D1_UnloadPlaceEntity oldEntity = zsj_d1_unloadplaceApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_d1_unloadplaceApp.DeleteForm(oldEntity.UnitID);
                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的卸货地点数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }
    }
}
